package info.javafans.cdn.database;

import java.io.IOException;
import java.sql.Connection;
import java.util.Properties;

import org.apache.log4j.Logger;

public class ConnectionPool {

	//ConnectionPool的单例引用
	private static ConnectionPool instance = new ConnectionPool();
	private Properties properties = null;	//配置文件
	private DbConn dbConn = null;
	private Logger logger = Logger.getLogger(ConnectionPool.class);
	
	private ConnectionPool() {
		init2();
	}

	public static ConnectionPool getInstance() {
		return instance;
	}
	
	private void init2() {
		
		properties = new Properties();
		try {
			//读取配置文件
			properties.load(ConnectionPool.class.getResourceAsStream("/database.properties"));
			//确定连接方式
			
			String conntype = properties.getProperty("conntype");
			Object o = Class.forName("info.javafans.cdn.database.JdbcConn").newInstance();
			dbConn = (DbConn)o;
			
		} catch (IOException e) {
			logger.error("database.properties读取失败");
		} catch (ClassNotFoundException e) {
			logger.error("没有找到类");
		} catch (InstantiationException e) {
			logger.error(e.getMessage());
		} catch (IllegalAccessException e) {
			logger.error(e.getMessage());
		}
	}
	
	/**
	 * 获取数据库连接
	 * @return
	 */
	public Connection getConnection() {
		Connection conn = null;
		if (dbConn == null) {
			System.out.println("dbConn is null");
		}
			conn = dbConn.getConnection(properties);		
		return conn;
	}

}
